import {request} from "../../request/index.js"

const app = getApp();


Page({
  data: {
    show: false,
    show2: false,
    show3: false,
    text: '请选择母线',
    text2: '请选择出线',
    text3: '请选择线路',
    one: [],
    num: '',
    line: '',
    userName: '',
    lineList1: [],
    lineList2: [],
    lineList3: [],
    busBar: '',
    superior: '',
    stationAndBusbar: '',
    superior35kv: '',
    belong10kv: '',
    line1: '',
    line2: '',
    line3: '',
    index: '',
    },
  onLoad(options){
    this.setData({
      index: options.index
    })
  },
  onShow() {
    request({
      url: '/api/list/line/tree',
      data: {
      },
      header: {

      }
    })
    .then(res => {
      console.log(res)
      var primaryList = res.data.data
      var primary = []
      var onePrimary = []
      primaryList.forEach((item, i) => {
        primary.push(item)
      })
      this.setData({
        one: primary
      })
    })
  },
 
  bindShowMsg() {
    this.setData({
      show: !this.data.show,
      text2: '请选择出线',
      text3: '请选择出线',
      lineList3: [],
      lineList2: []
    })

    request({
      url: "/api/list/busbar",
      data: {}
    })
    .then(res => {
      this.setData({
        lineList1: res.data.data
      })
    })

  },
  bindShowMsg2() {
    this.setData({
      show2: !this.data.show2,
      text3: '请选择出线',
      lineList3: []
    })
  },
  bindShowMsg3() {
    this.setData({
      show3: !this.data.show3
    })
  },
  selectItem(e) {
    var busBar = e.currentTarget.dataset.name
    this.setData({
      stationAndBusbar: busBar
    })
    if (busBar.length == 0 || busBar == undefined) {
      return false;
    } else {
      request({
        url: "/api/list/superior/?busBar=" + busBar,
        data: {

        }
      })
      .then(res => {
        this.setData({
          lineList2: res.data.data
          
        })
      })
      if(busBar.length > 6) {
        busBar = busBar.substring(0,6) + "...";
        this.setData({
          text: busBar,
          show: false
        })
      } else {
        this.setData({
          text: busBar,
          show: false
        })
      }  
    } 
    
    
  },
  selectItem2(e) {
    // console.log(e)
    var superior = e.currentTarget.dataset.name
    this.setData({
      superior35kv: superior
    })
    if (superior.length == 0 || superior == undefined) {
      return false;
    } else {
      request({
        url: "/api/list/belong/?superior=" + superior,
        data: {

        }
      })
      .then(res => {
        // console.log(res)
        var data = res.data.data
        this.setData({
          lineList3: data
        })
        
      })

      if(superior.length > 6) {
        superior = superior.substring(0,6) + "...";
        this.setData({
          text2: superior,
          show2: false
        })
      } else {
        this.setData({
          text2: superior,
          show2: false
        })
      }
    }
    
  },
  selectItem3(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      line: name,
      belong10kv: name
    })
    if (name.length == 0 || name == undefined) {
      return false;
    } else if(name.length > 6) {
      name = name.substring(0,6) + "...";
      this.setData({
        text3: name,
        show3: false
      })
    } else {
      this.setData({
        text3: name,
        show3: false
      })
    }
  },
  getName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  query() {
    if(this.data.index == 2){
      var that = this
      request({
        url: '/client/query/plan/' + app.userId,
        method: 'POST',
        data: {
          stationAndBusbar: that.data.stationAndBusbar,
          superior35kv: that.data.superior35kv,
          belong10kv: that.data.belong10kv
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        }
      })
      .then(res => {
        if(res.data.code === 500) {
          wx.showToast({
            title: res.data.message,
            mask: true,
            icon: 'none'
          })
        } else {
          if(res.data.length === 0) {
            wx.showToast({
              title: '当前线路没有数据',
              icon: 'none',
              mask: true
            })
            return
          } else {
            var scheme = []
            scheme.push(res.data)
            console.log(scheme)
            wx.navigateTo({
              url: '/pages/queryScheme/queryScheme?scheme=' + JSON.stringify(scheme) + '&text=' + that.data.text + '&text2=' + that.data.text2 + '&text3=' + that.data.text3 + '&line1=' + that.data.stationAndBusbar + '&line2=' + that.data.superior35kv + '&line3=' + that.data.belong10kv ,
            })
          }
        }
      })
    }
    if(this.data.index == 1){
      var that = this
      request({
        url: '/client/query/blackout/' + app.userId,
        method: 'POST',
        data: {
          stationAndBusbar: that.data.stationAndBusbar,
          superior35kv: that.data.superior35kv,
          belong10kv: that.data.belong10kv
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        }
      })
      .then(res => {
        if(res.data.code === 500) {
          wx.showToast({
            title: res.data.message,
            mask: true,
            icon: 'none'
          })
        } else {
          if(res.data.length === 0) {
            wx.showToast({
              title: '当前线路没有数据',
              icon: 'none',
              mask: true
            })
            return
          } else {
            var historyList = []
            historyList = res.data
            wx.navigateTo({
              url: '/pages/queryHistory/queryHistory?historyList=' + JSON.stringify(historyList) + '&stationAndBusbar=' + that.data.stationAndBusbar + '&superior35kv=' + that.data.superior35kv + '&belong10kv=' + that.data.belong10kv
            })
          }
        }
      })
    }
    if(this.data.index == 3) {
      var that = this
      request({
        url: '/client/query/plan/fault/' + app.userId,
        method: 'POST',
        data: {
          stationAndBusbar: this.data.stationAndBusbar,
          superior35kv: this.data.superior35kv,
          belong10kv: this.data.belong10kv
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        }
      })
      .then(res => {
        console.log(res)
        console.log(res.data.code)
        if(res.data.code === 500) {
          wx.showToast({
            title: res.data.message,
            mask: true,
            icon: 'none'
          })
        } else {
          if(res.data.length === 0) {
            wx.showToast({
              title: '当前线路没有数据',
              icon: 'none',
              mask: true
            })
            return
          } else {
            var updateList = []
            updateList = res.data
            wx.navigateTo({
              url: '/pages/updateProgress/updateProgress?updateList=' + JSON.stringify(updateList) + '&text=' + that.data.text + '&text2=' + that.data.text2 + '&text3=' + that.data.text3 + '&line1=' + that.data.stationAndBusbar + '&line2=' + that.data.superior35kv + '&line3=' + that.data.belong10kv
            })
          }
          
        }
      })
    }

    if(this.data.index == 4){
      var that = this
      request({
        url: '/api/list/all/comment',
        data: {
          stationAndBusbar: that.data.stationAndBusbar,
          superior35kv: that.data.superior35kv,
          belong10kv: that.data.belong10kv
        }
      })
      .then(res => {
        if(res.data.code === 500) {
          wx.showToast({
            title: res.data.message,
            mask: true,
            icon: 'none'
          })
        } else {
          if(res.data.data.length === 0) {
            wx.showToast({
              title: '当前线路没有数据',
              icon: 'none',
              mask: true
            })
            return
          } else {
            var userBack = []
            userBack = res.data.data
            wx.navigateTo({
              url: '/pages/back/back?userBack=' + JSON.stringify(userBack) 
              // + '&stationAndBusbar=' + that.data.stationAndBusbar + '&superior35kv=' + that.data.superior35kv + '&belong10kv=' + that.data.belong10kv
            })
          }
        }
      })
    }
  }
})